%%--------------------------------------------------------------------
%% Author: <vjache>
%% Created: 03.04.2009
%% Description: Vector abstract module.
%%--------------------------------------------------------------------

-module(vect).


%%--------------------------------------------------------------------
%% Include files
%%--------------------------------------------------------------------

-include("../include/vect.hrl").

%%--------------------------------------------------------------------
%% Exported Functions
%%--------------------------------------------------------------------
-export([
		 open/1,
		 open_pipe/1,
		 close/1,
		 delete/2,
		 insert/2,
		 lookup/2,
		 delete_key/2,
		 all/1,
		 clear/1
		 ]).

%%--------------------------------------------------------------------
%% API Functions
%%--------------------------------------------------------------------

open(VectDescr) when is_record(VectDescr, vect) ->
	OpenFun=VectDescr#vect.open_fun,
	{VectDescr,_VectSess=call(VectDescr,OpenFun,[])}.
close({VectDescr,VectSess}) ->
	Fun=VectDescr#vect.close_fun,
	ok=call(VectDescr,Fun,[VectSess]).
delete({VectDescr,VectSess}, Tuple) ->
	Fun=VectDescr#vect.delete_fun,
	ok=call(VectDescr,Fun,[VectSess,Tuple]).
insert({VectDescr,VectSess},TupleOrTupleList) -> 
	Fun=VectDescr#vect.insert_fun,
	ok=call(VectDescr,Fun,[VectSess,TupleOrTupleList]).
lookup({VectDescr,VectSess}, Key) -> %% iter()
	Fun=VectDescr#vect.lookup_fun,
	call(VectDescr,Fun,[VectSess,Key]).
open_pipe({VectDescr,VectSess}) ->
	Fun=VectDescr#vect.open_pipe_fun,
	call(VectDescr,Fun,[VectSess]).
delete_key({VectDescr,VectSess}, Key) ->
	Fun=VectDescr#vect.delete_key_fun,
	ok=call(VectDescr,Fun,[VectSess,Key]).
all({VectDescr,VectSess}) -> %% iter()
	Fun=VectDescr#vect.all_fun,
	call(VectDescr,Fun,[VectSess]).
clear({VectDescr,VectSess}) ->
	Fun=VectDescr#vect.clear_fun,
	ok=call(VectDescr,Fun,[VectSess]).



%%--------------------------------------------------------------------
%% Local Functions
%%--------------------------------------------------------------------


call(_VectDescr,Fun,Args) when is_function(Fun) ->
	apply(Fun,Args);
call(VectDescr,{FunName,InnerArgs},[]) when is_atom(FunName) ->
	call(VectDescr,FunName,InnerArgs);
call(VectDescr,{FunName,InnerArgs},Args) when is_atom(FunName) ->
	call(VectDescr,FunName,InnerArgs++Args);
call(VectDescr,FunName,Args) when is_atom(FunName) ->
	apply(VectDescr#vect.drv_module, FunName, Args).


